<cfmodule template="../../tag/layout.cfm" bodyIsEasyUILayout="false">
<script type="text/javascript" charset="utf-8" src="ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="ueditor/lang/zh-cn/zh-cn.js"></script>
<cfscript>
	if( structKeyExists(rc, "id")  && rc.id > 0 ){
		id = rc.id;
	}else{
		id = 0;
	}
</cfscript>

<form method="post" class="form">
  <div title="基本信息" style="padding:10px">
    <table class="table" id="table" name="table" width="100%">
      <tbody>
        <tr>
          <th>编号</th>
          <td><input name="id" readonly /></td>
        </tr>
        <tr>
          <th>标题</th>
          <td><input id="subject" name="subject" class="easyui-validatebox" data-options="required:true" style=" width:500px;"/></td>
        </tr>
        
        <tr class="userSelector">
          <th>组织机构</th>
          <td>
          	<a href="javascript:void(0);" id="btnOrganization" class="easyui-linkbutton" onclick="organizationFun()" data-options="iconCls:'ext-icon-vcard',plain:true">选择组织机构</a>
          	<input type="hidden" id="organizationids" name="organizationids">
          	<input type="text" id="organizationnames" name="organizationnames" style="border:none; width:400px; background: transparent;" readonly="readonly" value="请选择组织机构！">
          </td>
        </tr>
        
        <tr class="userSelector">
          <th>用户角色</th>
          <td>
          	<a href="javascript:void(0);" id="btnRole" class="easyui-linkbutton" onclick="roleFun()" data-options="iconCls:'ext-icon-group',plain:true">选择用户角色</a>
          	<input type="hidden" id="roleids" name="roleids">
          	<input type="text" id="rolenames" name="rolenames" style="border:none; width:400px; background: transparent;" readonly="readonly" value="请选择用户角色！">
          </td>
        </tr>
        
        <tr class="userSelector">
          <th>指定人员</th>
          <td>
          	<a href="javascript:void(0);" id="btnUser" class="easyui-linkbutton" onclick="userFun()" data-options="iconCls:'ext-icon-user',plain:true">选择指定用户</a>
          	<input type="hidden" id="userids" name="userids">
          	<input type="text" id="usernames" name="usernames" style="border:none; width:400px; background: transparent;" readonly="readonly" value="请选择指定用户！">
          </td>
        </tr>
        
         <tr class="userSelector">
          <th>全部人员</th>
          <td>
          	<input type="checkbox" id="alluser" name="alluser" value="1">
          </td>
        </tr>
        
        <tr>
          <th>内容</th>
          <td>
          	<textarea id="content" name="content" style="width:500px; height:180px;" class="easyui-validatebox" data-options="required:true"></textarea>
          	<input type='button' mode='0' class='editor' onclick="EditorRender('content',$(this))" value='切换至编辑器'>
          </td>
        </tr>
        
      </tbody>
    </table>
  </div>
</form>

</cfmodule>
<script type="text/javascript">
	var closeDialog = function($dialog) {
		$dialog.dialog('destroy');
	}
	
	var readDialog = function($dialog,$grid) {
		$grid.datagrid('reload');
		$dialog.dialog('destroy');
	}
	
	var replyDialog = function($dialog,$grid) {
		var smsid = $(':input[name="id"]').val();
		var dialog = parent.fw.modalDialog({
	        title: '回复记录',
	        url: '?action=sms.reply&smsid=' + smsid,
	        buttons: [
	        {
	            text: '回复消息',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.submitForm(dialog);
	            }
	        },{
	            text: '关闭窗口',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
	            }
	        }]
	    });
	}
	
	var organizationFun = function() {
		var orgids = $('#organizationids');
		var orgnames = $('#organizationnames');
		
		var url = '';
		if( orgids.val().trim().length > 0  ){
			url = "?action=sms.organization&ids=" + orgids.val();			
		}else{
			url = "?action=sms.organization";
		}
		
	    var dialog = parent.fw.modalDialog({
	        title: '选择机构',
	        url: url,
	        buttons: [
	        {
	            text: '选择机构',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.submitForm(dialog,orgids,orgnames);
	            }
	        },{
	            text: '关闭窗口',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
	            }
	        }]
	    });
	};
	
	var roleFun = function() {
		
		var roleids = $('#roleids');
		var rolenames = $('#rolenames');
		var url = '';
		if( roleids.val().trim().length > 0  ){
			url = "?action=sms.role&ids=" + roleids.val();			
		}else{
			url = "?action=sms.role";
		}
		
	    var dialog = parent.fw.modalDialog({
	        title: '选择角色',
	        url: url,
	        buttons: [
	        {
	            text: '选择角色',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.submitForm(dialog,roleids,rolenames);
	            }
	        },{
	            text: '关闭窗口',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
	            }
	        }]
	    });
	};
	
	
	var userFun = function() {
		
		var userids = $('#userids');
		var usernames = $('#usernames');
		var url = '';
		if( userids.val().trim().length > 0  ){
			url = "?action=sms.user&ids=" + userids.val();			
		}else{
			url = "?action=sms.user";
		}
	    var dialog = parent.fw.modalDialog({
	        title: '选择用户',
	        url: url,
	        buttons: [
	        {
	            text: '选择用户',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.submitForm(dialog,userids,usernames);
	            }
	        },{
	            text: '关闭窗口',
	            handler: function() {
	                dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
	            }
	        }]
	    });
	};
	
	var submitNow = function($dialog, $grid, $pjq) {
		var url;
		if ($(':input[name="id"]').val().length > 0) {
			url = '?action=sms.update'; //更新操作
		} else {
			url = '?action=sms.save';	//新增操作
		}

		$.post(url, fw.serializeObject($('form')), function(result) {
			if (result) {
				$grid.datagrid('reload');
				$dialog.dialog('destroy');
			}
		}, 'json');
	};
	
	var submitForm = function($dialog, $grid, $pjq) {
		//验证前先把编辑器转成编辑框
		if($('.editor').attr('mode') == 1){
			$('.editor').click();
		}
		
		if ($('form').form('validate')) {
			
			if($('#alluser').prop('checked')){
				//提交前先确认一共给多少用户进行消息发送，并做一统计数据进行提示确认		
				$.ajax({
				    type: "POST",
				    url: "?action=user.getUserCount"
				}).done(function(result) {
					//减1的目的是要将自己排除
					if( (result-1)>0){
						$.messager.confirm('确认', '你将向(' + (result-1) + ')个人员发送消息，是否确认？', function(r){
			                if (r){
								submitNow($dialog, $grid, $pjq);
			                }
			            });
					}
				}, 'json');
				
			}else{
				//提交前先确认一共给多少用户进行消息发送，并做一统计数据进行提示确认		
				$.ajax({
				    type: "POST",
				    url: "?action=user.getOrgRoleUser",
				    data: {
				        organizationids: $('#organizationids').val(),
				        roleids: $('#roleids').val() ,
				        userids: $('#userids').val()
				    }
				}).done(function(result) {
					if(result.trim().length > 0){
						$.messager.confirm('确认', '你将向(' + result.split(',').length + ')个人员发送消息，是否确认？', function(r){
			                if (r){
								submitNow($dialog, $grid, $pjq);
			                }
			            });
		            }else{
		            	 $.messager.alert('提示','你选的对象中不存在任何用户，请重选！','warning');
		            }
				}, 'json');
				
			} // alluser checked
		
		} // validate
		
	};
	
	<cfif id gt 0>
	$(function(){
		$.ajax({
		    type: "POST",
		    url: "?action=sms.get",
		    data: {
		        id:<cfoutput>#id#</cfoutput>
		    }
		}).done(function(result) {
			$('.userSelector').hide();
			$('form').form('load', result);
			if($('.editor').attr('mode') == 0){
				$('.editor').click();
			}
		}, 'json');
	})
	</cfif>	
</script>	 
